{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 2.3 标识符和关键字\n",
    "### 2.3.1 标识符\n",
    "### 命名规则\n",
    "    1.标示符由字母、下划线和数字组成，且数字不能开头。\n",
    "    2.Python中的标识符是区分大小写的。\n",
    "    3.python中的标示符不能使用关键字\n",
    "### 建议\n",
    "    1.见名知意\n",
    "    2.驼峰式\n",
    "    （如：userName,userLoginFlag)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.3.2 关键字\n",
    "    如：>>> help()          # 进入帮助系统\n",
    "       help> keywords      # 查看所有的关键字列表\n",
    "       help> return        # 查看return这个关键字的说明\n",
    "       help> quit          # 退出帮助系统\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.4 简单数值类型"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "整型"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "160\n"
     ]
    }
   ],
   "source": [
    "a = 14\n",
    "print(a)\n",
    "a = 0b1101\n",
    "print(a)\n",
    "a = 0o75\n",
    "print(a)\n",
    "a = 0xa0\n",
    "print(a)\n",
    "# 从上到下依次是十进制、二进制、八进制、十六进制"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T20:40:53.261775Z",
     "end_time": "2024-10-16T20:40:53.271936Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "浮点型"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3140000.0\n"
     ]
    }
   ],
   "source": [
    "a = 3.14e6\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T20:40:52.325315Z",
     "end_time": "2024-10-16T20:40:52.331857Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "布尔类型"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "a = 0\n",
    "print(bool(a))\n",
    "s = ''\n",
    "print(bool(s))\n",
    "lst = []\n",
    "print(bool(lst))\n",
    "dic = {}\n",
    "print(bool(dic))\n",
    "a = None\n",
    "print(bool(a))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T18:58:50.490229Z",
     "end_time": "2024-10-16T18:58:50.499097Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "复数类型"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5+3j)\n",
      "5.0\n",
      "3.0\n",
      "(2+0j)\n"
     ]
    }
   ],
   "source": [
    "a = 5 + 3j\n",
    "print(a)\n",
    "print(a.real)\n",
    "print(a.imag)\n",
    "b = 2\n",
    "b = complex(b)\n",
    "print(b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T20:42:18.757347Z",
     "end_time": "2024-10-16T20:42:18.766972Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "数字类型转换"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "2.0\n",
      "(2+0j)\n"
     ]
    }
   ],
   "source": [
    "a = int(3.14)\n",
    "print(a)\n",
    "b = float(2)\n",
    "print(b)\n",
    "c = 2\n",
    "c = complex(c)\n",
    "print(c)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T20:43:39.180379Z",
     "end_time": "2024-10-16T20:43:39.186618Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.5 运算符\n",
    "### 2.5.1 赋值运算符"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1 2\n",
      "3 2\n"
     ]
    }
   ],
   "source": [
    "a = 0\n",
    "a += 1\n",
    "a = a + 1\n",
    "print(a)\n",
    "a, b = 1, 2\n",
    "print(a, b)\n",
    "x = 2\n",
    "y = 3\n",
    "x, y = y, x\n",
    "print(x, y)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T20:46:24.045103Z",
     "end_time": "2024-10-16T20:46:24.053906Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.5.2 复合赋值运算符"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 - c的值为： 31\n",
      "2 - c的值为： 52\n",
      "3 - c的值为： 1092\n",
      "4 - c的值为： 52.0\n",
      "5 - c的值为： 2\n",
      "6 - c的值为： 2097152\n",
      "7 - c的值为： 99864\n"
     ]
    }
   ],
   "source": [
    "a = 21\n",
    "b = 10\n",
    "c = 0\n",
    "c = a + b\n",
    "print(\"1 - c的值为：\",c)\n",
    "c += a\n",
    "print(\"2 - c的值为：\",c)\n",
    "c *= a\n",
    "print(\"3 - c的值为：\",c)\n",
    "c /= a\n",
    "print(\"4 - c的值为：\",c)\n",
    "c = 2\n",
    "c %= a\n",
    "print(\"5 - c的值为：\",c)\n",
    "c **= a\n",
    "print(\"6 - c的值为：\",c)\n",
    "c //= a\n",
    "print(\"7 - c的值为：\",c)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T20:50:43.114510Z",
     "end_time": "2024-10-16T20:50:43.134567Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.5.3 逻辑运算符"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 - 变量 a 和 b 都为true\n",
      "2 - 变量 a 和 b 都为true，或其中一个变量为true\n",
      "3 - 变量 a 和 b 有一个不为true\n",
      "4 - 变量 a 和 b 都为true，或其中一个变量为true\n",
      "5 - 变量 a 和 b 都为false，或其中一个变量为false\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "b = 20\n",
    "if ( a and b):\n",
    "    print(\"1 - 变量 a 和 b 都为true\")\n",
    "else:\n",
    "    print(\"1 - 变量 a 和 b 有一个不为true\")\n",
    "if ( a or b):\n",
    "    print(\"2 - 变量 a 和 b 都为true，或其中一个变量为true\")\n",
    "else:\n",
    "    print(\"2 - 变量 a 和 b 都不为true\")\n",
    "a = 0\n",
    "if ( a and b ):\n",
    "    print(\"3 - 变量 a 和 b 都为true\")\n",
    "else:\n",
    "    print(\"3 - 变量 a 和 b 有一个不为true\")\n",
    "if ( a or b ):\n",
    "    print(\"4 - 变量 a 和 b 都为true，或其中一个变量为true\")\n",
    "else:\n",
    "    print(\"4 - 变量 a 和 b 都不为true\")\n",
    "if not ( a and b ):\n",
    "    print(\"5 - 变量 a 和 b 都为false，或其中一个变量为false\")\n",
    "else:\n",
    "    print(\"5 - 变量 a 和 b 都为true\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T20:55:52.708301Z",
     "end_time": "2024-10-16T20:55:52.715029Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.5.4 成员运算符"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 - 变量 a 不在给定的列表list中\n",
      "2 - 变量 b 不在给定的列表list中\n",
      "3 - 变量 a 在给定的列表list中\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "b = 20\n",
    "list = [1, 2, 3, 4, 5]\n",
    "if ( a in list ):\n",
    "    print(\"1 - 变量 a 在给定的列表list\"\n",
    "          \"中\")\n",
    "else:\n",
    "    print(\"1 - 变量 a 不在给定的列表list中\")\n",
    "if ( b not in list ):\n",
    "    print(\"2 - 变量 b 不在给定的列表list中\")\n",
    "else:\n",
    "    print(\"2 - 变量 b 在给定的列表list中\")\n",
    "# 修改变量a的值\n",
    "a = 2\n",
    "if ( a in list ):\n",
    "    print(\"3 - 变量 a 在给定的列表list\"\n",
    "          \"中\")\n",
    "else:\n",
    "    print(\"3 - 变量 a 不在给定的列表list中\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T20:58:31.820455Z",
     "end_time": "2024-10-16T20:58:31.828072Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.6 位运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 - c 的值为： 12\n",
      "2 - c 的值为： 61\n",
      "3 - c 的值为： 49\n",
      "4 - c 的值为： -61\n",
      "5 - c 的值为： 240\n",
      "6 - c 的值为： 15\n"
     ]
    }
   ],
   "source": [
    "a = 60 # 60 = 0011 1100\n",
    "b = 13 # 13 = 0000 1101\n",
    "c = 0\n",
    "c = a & b # 12 = 0000 1100\n",
    "print(\"1 - c 的值为：\", c)\n",
    "c = a | b # 61 = 0011 1111\n",
    "print(\"2 - c 的值为：\", c)\n",
    "c = a ^ b # 49 = 0011 0001\n",
    "print(\"3 - c 的值为：\", c)\n",
    "c = ~a # -61 = 1100 0011\n",
    "print(\"4 - c 的值为：\", c)\n",
    "c = a << 2 # 240 = 1111 0000\n",
    "print(\"5 - c 的值为：\", c)\n",
    "c = a >> 2 # 15 = 0000 1111\n",
    "print(\"6 - c 的值为：\", c)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T21:05:54.109837Z",
     "end_time": "2024-10-16T21:05:54.116704Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.7 运算符优先级"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "90.0\n",
      "90.0\n",
      "90.0\n",
      "50.0\n"
     ]
    }
   ],
   "source": [
    "a = 20\n",
    "b = 10\n",
    "c = 15\n",
    "d = 5\n",
    "e = 0\n",
    "e = (a + b) * c / d\n",
    "print(e)\n",
    "e = ((a + b) * c) / d\n",
    "print(e)\n",
    "e = (a + b) * (c / d)\n",
    "print(e)\n",
    "e = a + (b * c) / d\n",
    "print(e)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T21:08:03.838462Z",
     "end_time": "2024-10-16T21:08:03.844391Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 3 Python流程控制语句\n",
    "## 3.1 判断语句\n",
    "### 3.1.1 if语句\n",
    "### 3.1.2 if...else语句\n",
    "### 3.1.3 if...elif语句\n",
    "### 3.1.4 if嵌套语句"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "welcome boss\n",
      "roadman\n",
      "hello\n",
      "undefine\n",
      "hello\n"
     ]
    }
   ],
   "source": [
    "flag = False\n",
    "name = 'luren'\n",
    "if name == 'luren':\n",
    "    flag = True\n",
    "    print(\"welcome boss\")\n",
    "else:\n",
    "    print(name)\n",
    "num = 5\n",
    "if num == 3:\n",
    "    print(\"boss\")\n",
    "elif num == 2:\n",
    "    print(\"user\")\n",
    "elif num == 1:\n",
    "    print(\"worker\")\n",
    "elif num < 0:\n",
    "    print(\"error\")\n",
    "else:\n",
    "    print(\"roadman\")\n",
    "num = 9\n",
    "if num >= 0 and num <= 10:\n",
    "    print(\"hello\")\n",
    "num = 10\n",
    "if num < 0 or num > 10:\n",
    "    print(\"hello\")\n",
    "else:\n",
    "    print(\"undefine\")\n",
    "nume = 8\n",
    "if ( num >= 0 and num <= 5) or ( num >= 10 and num <= 15):\n",
    "    print(\"hello\")\n",
    "else:\n",
    "    print(\"undefine\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T21:16:28.844428Z",
     "end_time": "2024-10-16T21:16:28.853435Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.2 循环语句\n",
    "### 3.2.1 while语句"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the count is: 0\n",
      "the count is: 1\n",
      "the count is: 2\n",
      "the count is: 3\n",
      "the count is: 4\n",
      "the count is: 5\n",
      "the count is: 6\n",
      "the count is: 7\n",
      "the count is: 8\n",
      "Good bye!\n"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "while(count < 9):\n",
    "    print('the count is:', count)\n",
    "    count += 1\n",
    "print(\"Good bye!\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T21:19:33.127650Z",
     "end_time": "2024-10-16T21:19:33.135716Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.2.2 for语句"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "for letter in \"Python\":\n",
    "    print('当前字母：', letter)\n",
    "fruits = ['banana', 'apple', 'mango']\n",
    "for fruit in fruits:\n",
    "    print('当前水果：', fruit)\n",
    "print(\"Good bye!\")\n",
    "for num in range(10,20):\n",
    "    for i in range(2, num):\n",
    "        if num % i == 0:\n",
    "            j = num / i\n",
    "            print('%d 等于 %d * %d' % (num, i, j))\n",
    "            break\n",
    "    else:\n",
    "        print(num, '是一个质数')"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.2.3 while嵌套"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 是质数\n",
      "3 是质数\n",
      "5 是质数\n",
      "7 是质数\n",
      "11 是质数\n",
      "13 是质数\n",
      "17 是质数\n",
      "19 是质数\n",
      "23 是质数\n",
      "29 是质数\n",
      "31 是质数\n",
      "37 是质数\n",
      "41 是质数\n",
      "43 是质数\n",
      "47 是质数\n",
      "53 是质数\n",
      "59 是质数\n",
      "61 是质数\n",
      "67 是质数\n",
      "71 是质数\n",
      "73 是质数\n",
      "79 是质数\n",
      "83 是质数\n",
      "89 是质数\n",
      "97 是质数\n",
      "Good bye!\n"
     ]
    }
   ],
   "source": [
    "i = 2\n",
    "while i < 100:\n",
    "    j = 2\n",
    "    while j < (i / j):\n",
    "        if not (i % j): break\n",
    "        j += 1\n",
    "    if j > (i / j): print(i, \"是质数\")\n",
    "    i += 1\n",
    "print(\"Good bye!\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T21:21:48.952666Z",
     "end_time": "2024-10-16T21:21:48.961266Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.3 Python的其他语句\n",
    "### 3.3.1 break语句"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------\n",
      "1\n",
      "-------\n",
      "2\n",
      "-------\n"
     ]
    }
   ],
   "source": [
    "i=1\n",
    "for i in range(5):\n",
    "     i+=1\n",
    "     print(\"-------\")\n",
    "     if i==3:\n",
    "         break\n",
    "     print(i)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T21:22:48.103898Z",
     "end_time": "2024-10-16T21:22:48.110565Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.3.2 continue语句"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------\n",
      "1\n",
      "-------\n",
      "2\n",
      "-------\n",
      "-------\n",
      "4\n",
      "-------\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "i=1\n",
    "for i in range(5):\n",
    "     i+=1\n",
    "     print(\"-------\")\n",
    "     if i==3:\n",
    "         continue\n",
    "     print(i)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T21:23:15.753276Z",
     "end_time": "2024-10-16T21:23:15.760708Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.3.3 pass语句"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前字母 : R\n",
      "当前字母 : u\n",
      "当前字母 : n\n",
      "执行 pass 块\n",
      "当前字母 : o\n",
      "执行 pass 块\n",
      "当前字母 : o\n",
      "当前字母 : b\n",
      "Good bye!\n"
     ]
    }
   ],
   "source": [
    "for letter in 'Runoob':\n",
    "   if letter == 'o':\n",
    "      pass\n",
    "      print ('执行 pass 块')\n",
    "   print ('当前字母 :', letter)\n",
    "print (\"Good bye!\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T21:23:39.417478Z",
     "end_time": "2024-10-16T21:23:39.420844Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.3.4 else语句"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 is less than 5\n",
      "1 is less than 5\n",
      "2 is less than 5\n",
      "3 is less than 5\n",
      "4 is less than 5\n",
      "5 is not less than 5\n"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "while count < 5:\n",
    "    print(count, \"is less than 5\")\n",
    "    count += 1\n",
    "else:\n",
    "    print(count, \"is not less than 5\" )"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T21:25:16.362719Z",
     "end_time": "2024-10-16T21:25:16.365051Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 4 字符串\n",
    "## 4.1 字符串介绍\n",
    "### 4.1.1 转义字符\n",
    "    \\(在行尾时）续行\n",
    "    \\\" 双引号\n",
    "    \\\\ 反斜杠\n",
    "    \\n 换行符\n",
    "    \\t 制表符\n",
    "    \\b 退格符\n",
    "## 4.2 字符串的输出和输入\n",
    "    常见的格式化符号\n",
    "    %s 字符串类型\n",
    "    %d 整数类型\n",
    "    %f 小数类型"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4.2.1 字符串的输出"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "大家好，我叫小明 ,我今年5岁了\n"
     ]
    }
   ],
   "source": [
    "name = '小明'\n",
    "age = 5\n",
    "print(\"大家好，我叫%s ,我今年%d岁了\"%(name,age))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-16T21:31:00.424961Z",
     "end_time": "2024-10-16T21:31:00.458116Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4.2.2 字符串的输入"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n"
     ]
    }
   ],
   "source": [
    "username = input(\"请输入用户名\")\n",
    "print(username)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T18:37:36.684541Z",
     "end_time": "2024-10-23T18:37:41.923601Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "计算阶乘"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3628800\n"
     ]
    }
   ],
   "source": [
    "num = int(input(\"请输入数字：\"))\n",
    "outcome = 1\n",
    "while(num > 0):\n",
    "    outcome *= num\n",
    "    num -= 1\n",
    "print(outcome)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T18:41:13.894676Z",
     "end_time": "2024-10-23T18:41:17.762735Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.3使用切片截取字符串"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abc\n",
      "de\n",
      "bcde\n",
      "cdef\n",
      "fdb\n",
      "fedc\n"
     ]
    }
   ],
   "source": [
    "name = 'abcdef'\n",
    "print(name[0:3])\n",
    "print(name[3:5])\n",
    "print(name[1:-1])\n",
    "print(name[2:])\n",
    "print(name[::-2])\n",
    "print(name[:1:-1])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T18:44:11.705468Z",
     "end_time": "2024-10-23T18:44:11.710184Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.4字符串内建函数\n",
    "find函数：检测字符串是否包括子字符串，返回子字符串的索引位置，如果没有找到，返回-1\n",
    "str.find(string, beg = 0, end = len(str))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "-1\n"
     ]
    }
   ],
   "source": [
    "name =' abcdef'\n",
    "print(name.find('ab'))\n",
    "print(name.find('ba'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T18:55:10.993448Z",
     "end_time": "2024-10-23T18:55:10.998537Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "index函数：检测字符串是否包括子字符串，返回子字符串的索引位置，如果没有找到，报错\n",
    "str.index(string, beg = 0, end = len(str))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "name = 'abcdef'\n",
    "print(name.index('ab'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T18:58:34.615491Z",
     "end_time": "2024-10-23T18:58:34.645384Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "replace函数：将旧字符串替换为新字符串，如果max有值，则替换不超过max次\n",
    "str.replace(old, new[,max])"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Abcdef\n"
     ]
    }
   ],
   "source": [
    "name = 'abcdef'\n",
    "print(name.replace('a','A'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T19:02:25.536604Z",
     "end_time": "2024-10-23T19:02:25.565234Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "count函数：统计字符串中某个字符出现的次数\n",
    "str.count(str, beg = 0, end = len(str))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "name = 'helloworld'\n",
    "print(name.count('l'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T19:07:37.788512Z",
     "end_time": "2024-10-23T19:07:37.816455Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "split函数：通过指定分隔符对字符串进行切片，生成列表\n",
    "str.split(str=\"\", num=str.count(string))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1', '2', '3', '4', '5']\n"
     ]
    }
   ],
   "source": [
    "name = '1,2,3,4,5'\n",
    "print(name.split(','))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T19:09:22.904553Z",
     "end_time": "2024-10-23T19:09:22.931767Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "capitalize函数：将字符串的第一个字符大写，其余小写\n",
    "str.capitalize()\n",
    "titles函数：将字符串中每个单词的首字母大写，其余小写\n",
    "str.title()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world\n",
      "Hello World\n"
     ]
    }
   ],
   "source": [
    "name = 'hello world'\n",
    "print(name.capitalize())\n",
    "print(name.title())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T19:14:12.393817Z",
     "end_time": "2024-10-23T19:14:12.406838Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "startswith函数：检查字符串是否以指定子串开头，如果是返回True，否则返回False\n",
    "str.startswith(str, beg = 0, end = len(str))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "name = 'helloworld'\n",
    "print(name.startswith('hello'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T19:13:35.710324Z",
     "end_time": "2024-10-23T19:13:35.758661Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "endswith函数：检查字符串是否以指定子串结尾，如果是返回True，否则返回False\n",
    "str.endswith(suffix[,start[,end]])"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "name = 'helloworld'\n",
    "print(name.endswith('world'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T19:25:32.205244Z",
     "end_time": "2024-10-23T19:25:32.210259Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "upper函数：将字符串中的小写字母转换为大写字母\n",
    "str.upper()\n",
    "lower函数：将字符串中的大写字母转换为小写字母\n",
    "str.lower()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HELLO WORLD\n",
      "hello world\n"
     ]
    }
   ],
   "source": [
    "name = 'hello world'\n",
    "print(name.upper())\n",
    "name = 'HELLO WORLD'\n",
    "print(name.lower())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-24T17:42:08.374497Z",
     "end_time": "2024-10-24T17:42:08.379912Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "ljust函数：左对齐，并用空格填充至指定长度的新字符串\n",
    "str.ljust(width[,fillchar])"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello*****\n"
     ]
    }
   ],
   "source": [
    "name = 'hello'\n",
    "print(name.ljust(10,'*'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T19:33:53.719844Z",
     "end_time": "2024-10-23T19:33:53.726177Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "rjust函数：右对齐，并用空格填充至指定长度的新字符串\n",
    "str.rjust(width[,fillchar])"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*****hello\n"
     ]
    }
   ],
   "source": [
    "name = 'hello'\n",
    "print(name.rjust(10,'*'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T19:33:55.739103Z",
     "end_time": "2024-10-23T19:33:55.745627Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "center函数：居中，并用空格填充至指定长度的新字符串\n",
    "str.center(width[,fillchar])"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "**hello***\n"
     ]
    }
   ],
   "source": [
    "name = 'hello'\n",
    "print(name.center(10,'*'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T19:34:17.290938Z",
     "end_time": "2024-10-23T19:34:17.328854Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "lstrip函数：删除字符串左边的空格或指定字符\n",
    "str.lstrip([chars])"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    }
   ],
   "source": [
    "name = '    hello'\n",
    "print(name.lstrip())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T19:37:15.227978Z",
     "end_time": "2024-10-23T19:37:15.236136Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "strip函数：删除字符串左右两边的空格或指定字符\n",
    "str.strip([chars])"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    }
   ],
   "source": [
    "name = '   hello   '\n",
    "print(name.strip())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T19:38:22.424670Z",
     "end_time": "2024-10-23T19:38:22.432176Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.5字符串运算符\n",
    "    + 连接字符串\n",
    "    * 重复字符串\n",
    "    [] 提取字符\n",
    "    [:] 切片\n",
    "    in 检测字符串是否包含子串\n",
    "    not in 检测字符串是否不包含子串\n",
    "    r/R 字符串格式化"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcd\n",
      "efg\n",
      "abcd\\nefg\n"
     ]
    }
   ],
   "source": [
    "str = 'abcd\\nefg'\n",
    "print(str)\n",
    "print(r'abcd\\nefg')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T19:41:32.217045Z",
     "end_time": "2024-10-23T19:41:32.224145Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 5 列表，元组和字典\n",
    "## 5.1 列表概述\n",
    "    1.列表是Python中的一种数据结构，它可以存储不同类型的数据\n",
    "    2.列表索引是从0开始的，我们可以通过下标索引的方式来访问列表中的值。"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xiaoWang\n",
      "xiaoZhang\n"
     ]
    }
   ],
   "source": [
    "A = [1,'xiaoWang','a', [2, 'b']]\n",
    "A = ['xiaoWang', 'xiaoZhang', 'xiaoHua']\n",
    "print(A[0])\n",
    "print(A[1])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-25T16:14:16.752432Z",
     "end_time": "2024-10-25T16:14:16.757313Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 5.2列表的循环遍历\n",
    "### 5.2.1 for循环遍历"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xiaoWang\n",
      "xiaoZhang\n",
      "xiaoHua\n"
     ]
    }
   ],
   "source": [
    "namesList = ['xiaoWang', 'xiaoZhang', 'xiaoHua']\n",
    "for i in namesList:\n",
    "    print(i)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-25T16:15:28.469649Z",
     "end_time": "2024-10-25T16:15:28.478173Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 5.2.2 while循环遍历"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xiaoWang\n",
      "xiaoZhang\n",
      "xiaoHua\n"
     ]
    }
   ],
   "source": [
    "namesList = ['xiaoWang', 'xiaoZhang', 'xiaoHua']\n",
    "length = len(namesList)\n",
    "i = 0\n",
    "while i < length:\n",
    "    print(namesList[i])\n",
    "    i += 1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-25T16:19:11.104484Z",
     "end_time": "2024-10-25T16:19:11.112196Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 5.3 列表的常见操作\n",
    "append函数：向列表末尾添加元素"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3]\n",
    "a.append(4)\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T20:00:16.065949Z",
     "end_time": "2024-10-23T20:00:16.073339Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "extend函数：向列表末尾添加元素，与append不同的是，可以添加多个元素"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3]\n",
    "b = [4,5,6]\n",
    "a.extend(b)\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T20:02:02.744366Z",
     "end_time": "2024-10-23T20:02:02.753050Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "insert函数：在列表指定位置插入元素"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, [4, 5, 6], 2, 3]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3]\n",
    "b = [4,5,6]\n",
    "a.insert(1, b)\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-10-23T20:02:30.729937Z",
     "end_time": "2024-10-23T20:02:30.767585Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 5.4元组的相关操作"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
